package cc.rengu.redp.system.domain;

import cc.rengu.redp.common.converter.CustomConverter;
import cc.rengu.redp.common.converter.ExcelCustomConvert;
import cc.rengu.redp.common.domain.RegexpConstant;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.Date;

@Data
@TableName("T_USER")
@ExcelIgnoreUnannotated
public class User implements Serializable {

    private static final long serialVersionUID = -4852732617765810959L;
    public static final String ADMIN_USER_ID = "1";
    /**
     * 账户状态
     */
    public static final String STATUS_VALID = "1";

    public static final String STATUS_LOCK = "0";

    public static final String DEFAULT_AVATAR = "default.jpg";

    /**
     * 性别
     */
    public static final String SEX_MALE = "0";

    public static final String SEX_FEMALE = "1";

    public static final String SEX_UNKNOW = "2";

    @TableId(value = "USER_ID")
    private Long userId;

    @Size(min = 4, max = 10, message = "{range}")
    @ExcelProperty(value = "用户名")
    private String username;

    @ExcelProperty(value = "中文名")
    private String chnName;

    @ExcelProperty(value = "英文名")
    private String enName;

    @JSONField(serialize = false)
    private String password;

    private Long deptId;

    private Long orgId;

    @ExcelProperty(value = "部门")
    @TableField(exist = false)
    private String deptName;

    @ExcelProperty(value = "机构链")
    @TableField(exist = false)
    private String orgChain;

    @ExcelProperty(value = "所属机构名称")
    @TableField(exist = false)
    private String orgName;

    @ExcelProperty(value = "所属机构编码")
    @TableField(exist = false)
    private String orgCode;

    @Size(max = 50, message = "{noMoreThan}")
    @Email(message = "{email}")
    @ExcelProperty(value = "邮箱")
    private String email;

    @Pattern(regexp = RegexpConstant.MOBILE_REG, message = "{mobile}")
    @ExcelProperty(value = "手机号")
    private String mobile;

    @NotBlank(message = "{required}")
    @ExcelProperty(value = "状态", converter = CustomConverter.class)
    @ExcelCustomConvert(value = {"0", "1"}, label = {"锁定", "有效"})
    private String status;

    @ExcelProperty(value = "创建时间")
    private Date createTime;

    @ExcelProperty(value = "修改时间")
    private Date modifyTime;

    @ExcelProperty(value = "最后登录时间")
    private Date lastLoginTime;

    @NotBlank(message = "{required}")
    @ExcelProperty(value = "性别", converter = CustomConverter.class)
    @ExcelCustomConvert(value = {"0", "1", "2"}, label = {"男", "女", "保密"})
    private String ssex;

    @Size(max = 100, message = "{noMoreThan}")
    @ExcelProperty(value = "个人描述")
    private String description;

    private String avatar;

    @NotBlank(message = "{required}")
    @TableField(exist = false)
    private String roleId;

    @ExcelProperty(value = "角色")
    @TableField(exist = false)
    private String roleName;

    // 排序字段
    @TableField(exist = false)
    private String sortField;

    // 排序规则 ascend 升序 descend 降序
    @TableField(exist = false)
    private String sortOrder;

    @TableField(exist = false)
    private String createTimeFrom;

    @TableField(exist = false)
    private String createTimeTo;

    @TableField(exist = false)
    private String id;

    /**
     * shiro-redis v3.1.0 必须要有 getAuthCacheKey()或者 getId()方法
     * # Principal id field name. The field which you can get unique id to identify this principal.
     * # For example, if you use UserInfo as Principal class, the id field maybe userId, userName, email, etc.
     * # Remember to add getter to this id field. For example, getUserId(), getUserName(), getEmail(), etc.
     * # Default value is authCacheKey or id, that means your principal object has a method called "getAuthCacheKey()" or "getId()"
     *
     * @return userId as Principal id field name
     */
    public Long getAuthCacheKey() {
        return userId;
    }

    public enum UserStatusEnum{

        COMMON("00","正常"),
        CANCEL("01","注销"),
        BLOCK("02","冻结")
        ;
        private String type;
        private String desc;

        UserStatusEnum(String type, String desc) {
            this.type = type;
            this.desc = desc;
        }

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }
}
